{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "class Person(object):\r\n",
    "    def __init__(self, name, age, taste):\r\n",
    "        self.name = name\r\n",
    "        self._age = age\r\n",
    "        self.__taste = taste\r\n",
    "\r\n",
    "    def showperson(self):\r\n",
    "        print(self.name)\r\n",
    "        print(self._age)\r\n",
    "        print(self.__taste)\r\n",
    "\r\n",
    "    def dowork(self):\r\n",
    "        self._work()\r\n",
    "        self.__away()\r\n",
    "\r\n",
    "\r\n",
    "    def _work(self):\r\n",
    "        print('my _work')\r\n",
    "\r\n",
    "    def __away(self):\r\n",
    "        print('my __away')"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "class Student(Person):\r\n",
    "    # def __init__(self, name, age, taste):\r\n",
    "    #     self.name = name\r\n",
    "    #     self._age = age\r\n",
    "    #     self.__taste = taste\r\n",
    "    def construction(self, name, age, taste):\r\n",
    "        self.name = name\r\n",
    "        self._age = age\r\n",
    "        self.__taste = taste\r\n",
    "\r\n",
    "    def showstudent(self):\r\n",
    "        print(self.name)\r\n",
    "        print(self._age)\r\n",
    "        print(self.__taste)\r\n",
    "\r\n",
    "    @staticmethod\r\n",
    "    def testbug():\r\n",
    "        _Bug.showbug()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "# 模块内可以访问，当from  cur_module import *时，不导入\r\n",
    "class _Bug(object):\r\n",
    "    @staticmethod\r\n",
    "    def showbug():\r\n",
    "        print(\"showbug\")"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "s1 = Student('jack', 25, 'football')\r\n",
    "s1.showstudent()  # 调用子类的 showstudent() 方法\r\n",
    "'''\r\n",
    "注意：子类当中没有init方法（已被注释掉），所以对其调用的是父类的初始化方法，使用 showstudeng() 访问不了这个私有属性\r\n",
    "'''\r\n",
    "\r\n",
    "print('*'*20)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "jack\n",
      "25\n"
     ]
    },
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'Student' object has no attribute '_Student__taste'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-8b39c8685ca6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ms1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mStudent\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'jack'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m25\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'football'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ms1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshowstudent\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 调用子类的 showstudent() 方法\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m '''\n\u001b[0;32m      4\u001b[0m \u001b[0m注意\u001b[0m\u001b[0;31m：\u001b[0m\u001b[0m子类当中没有init方法\u001b[0m\u001b[0;31m（\u001b[0m\u001b[0m已被注释掉\u001b[0m\u001b[0;31m）\u001b[0m\u001b[0;31m，\u001b[0m\u001b[0m所以对其调用的是父类的初始化方法\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m '''\n",
      "\u001b[1;32m<ipython-input-17-0ac3187b26a9>\u001b[0m in \u001b[0;36mshowstudent\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_age\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__taste\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mstaticmethod\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Student' object has no attribute '_Student__taste'"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "source": [
    "s1 = Student('jack', 25, 'football')\r\n",
    "s1.showperson()  # 调用父类的 showperson() 方法\r\n",
    "print('*'*20)\r\n",
    "\r\n",
    "# 无法访问__taste,导致报错\r\n",
    "s1.showstudent()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "jack\n",
      "25\n",
      "football\n",
      "********************\n",
      "jack\n",
      "25\n"
     ]
    },
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'Student' object has no attribute '_Student__taste'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-b4ffb9b3ba38>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m# 无法访问__taste,导致报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0ms1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshowstudent\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-17-0ac3187b26a9>\u001b[0m in \u001b[0;36mshowstudent\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_age\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__taste\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mstaticmethod\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Student' object has no attribute '_Student__taste'"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "source": [
    "s1.construction('rose', 30, 'basketball')  # 调用自己（子类）的 construction() 方法\r\n",
    "s1.showperson()\r\n",
    "print('*'*20)\r\n",
    "\r\n",
    "s1.showstudent()\r\n",
    "print('*'*20)\r\n",
    "\r\n",
    "Student.testbug()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "rose\n",
      "30\n",
      "football\n",
      "********************\n",
      "rose\n",
      "30\n",
      "basketball\n",
      "********************\n",
      "showbug\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.6.4",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}